package com.imooc.servlet.cookie;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//测试Cookie，创建cookie对象，设置cookie有效期，添加cookie信息到响应中。
//request.getCookies()方法获取所有的cookie信息

@WebServlet("/cookies/login")
public class ImoocLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("用户登录成功");
		//Cookie cookie=new cookie("cookie名称","数据")
		Cookie cookie = new Cookie("user", "admin");
		//设置cookie有效期为7天
		cookie.setMaxAge(60*60*24*7);
		//addCookie(Cookie对象)方法添加cookie，将cookie信息随着响应发到浏览器端
		//浏览器把cookie保存到本地文件中
		response.addCookie(cookie);
		//在浏览器中创建一个名为user，值为admin的cookie
		response.getWriter().println("login success");
		
	}

}
